package com.medlinker.android.adapters;

import com.medlinker.android.R;
import com.medlinker.android.activity.ViewPagerActivity;
import com.medlinker.android.viewpagerindicator.TitleProvider;



import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

public class ViewPagerAdapter extends PagerAdapter implements TitleProvider{

    private String [] pager_titles;
    private final ViewPagerActivity pagerActivity;
    
    public ViewPagerAdapter(ViewPagerActivity context){
        pagerActivity = context;
        pager_titles = new String[]{
                pagerActivity.getString(R.string.main),
                pagerActivity.getString(R.string.by_disease_search),
                pagerActivity.getString(R.string.by_hospital_search),
                pagerActivity.getString(R.string.search),
                pagerActivity.getString(R.string.my_user)
        };
    }
    
    @Override
    public void destroyItem(View pager, int position, Object view) {
        try{
            ((ViewPager)pager).removeView((View)view);
        }catch(Exception e){
        }
    }
    
    @Override
    public void finishUpdate(View container) {
    }
    
    @Override
    public int getCount() {
        return pager_titles.length;
    }

    @Override
    public Object instantiateItem(View pager, int position) {
        View v = null;
        LayoutInflater inflater = (LayoutInflater)pagerActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(position == 0){
            v = inflater.inflate(R.layout.main_pager1, null);
        }else if(position == 1){
            v = inflater.inflate(R.layout.main_pager2, null);
        }else if(position == 2){
            v = inflater.inflate(R.layout.main_pager3, null);
        }else if(position == 3){
            v = inflater.inflate(R.layout.main_pager4, null);
        }else if(position == 4){
            v = inflater.inflate(R.layout.main_pager5, null);
        }
        ((ViewPager)pager).addView( v, 0 );
        return v;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }

    @Override
    public String getTitle(int position) {
        return pager_titles[position];
    }

}
